﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;

namespace EXPrinter
{
    internal class IniConfigFile
    {
        public static string AssemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Substring(6);
       
        public static string IniFileName = Path.Combine(AssemblyDirectory, "PhotoPrint.ini");

        #region 读取INI文件
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static string IniValue(string IniKey)
        {        
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString("base", IniKey, "", temp, 500, IniFileName);
            return temp.ToString();
        }

        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static string IniValue(string section, string key)
        {
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(section, key, "", temp, 500, IniFileName);
            return temp.ToString();
        }

        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        #endregion

        #region 写INI文件
        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static long WriteValue(string IniKey, string NewValue)
        {
            long i = WritePrivateProfileString("base", IniKey, NewValue, IniFileName);
            return i;
        }

        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <param name="NewValue">新值</param>
        /// <returns></returns>
        public static long WriteValue(string section, string IniKey, string NewValue)
        {
            long i = WritePrivateProfileString(section, IniKey, NewValue, IniFileName);
            return i;
        }

        [DllImport("kernel32")]
        public static extern long WritePrivateProfileString(
            string section, string key, string val, string filePath);
        #endregion
    }
}